package com.jhclothing.outbound.service.impl;

import com.jhclothing.common.enums.ClothingSize;
import com.jhclothing.outbound.domain.ClothingOutbound;
import com.jhclothing.outbound.mapper.ClothingOutboundMapper;
import com.jhclothing.outbound.service.IClothingOutboundService;
import com.jhclothing.product.domain.ClothingInventory;
import com.jhclothing.product.domain.ClothingType;
import com.jhclothing.product.service.IClothingInventoryService;
import com.jhclothing.product.service.IClothingTypeService;
import com.jhclothing.product.vo.ClothingInventoryVo;
import com.jhclothing.product.vo.StatisticInOutVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 服饰出库Service业务层处理
 *
 * @author zyp
 * @date 2024-04-25
 */
@Service
public class ClothingOutboundServiceImpl implements IClothingOutboundService {
    @Autowired
    private ClothingOutboundMapper clothingOutboundMapper;
    @Autowired
    private IClothingInventoryService clothingInventoryService;
    @Autowired
    private IClothingTypeService clothingTypeService;

    /**
     * 查询服饰出库
     *
     * @param outboundId 服饰出库主键
     * @return 服饰出库
     */
    @Override
    public ClothingOutbound selectClothingOutboundByOutboundId(Long outboundId) {
        return clothingOutboundMapper.selectClothingOutboundByOutboundId(outboundId);
    }

    /**
     * 查询服饰出库列表
     *
     * @param clothingoutbound 服饰出库
     * @return 服饰出库
     */
    @Override
    public List<ClothingOutbound> selectClothingOutboundList(ClothingOutbound clothingoutbound) {
        return clothingOutboundMapper.selectClothingOutboundList(clothingoutbound);
    }

    /**
     * 统计一定时间内的服饰入库数量
     */
    @Override
    public List<StatisticInOutVo> statisticClothingOutboundNum(StatisticInOutVo statisticInOutVo) {
        return clothingOutboundMapper.statisticClothingOutboundNum(statisticInOutVo);
    }

    /**
     * 新增服饰出库
     *
     * @param clothingOutbound 服饰出库
     * @return 结果
     */
    @Override
    @Transactional
    public String insertClothingOutbound(ClothingOutbound clothingOutbound) {
        ClothingInventory clothing = clothingInventoryService.selectClothingInventoryByClothingTypeAndSize(
                clothingOutbound.getClothingTypeId(), clothingOutbound.getClothingSize());

        ClothingType clothingType = clothingTypeService.selectClothingTypeByClothingTypeId(clothingOutbound.getClothingTypeId());
        String clothingSize = ClothingSize.getClothingSizeNameByCode(clothingOutbound.getClothingSize());

        if (null == clothing) {
            return String.format("当前选择的服饰品类: %s，服饰尺码为： %s 对应的服饰不存在", clothingType.getClothingTypeCodeAndSex(), clothingSize);
        }
        int clothingInventory = clothing.getNowInventory() - clothingOutbound.getOutboundNum();
        if (clothingInventory < 0) {
            return String.format("服饰品类为%s, 服饰尺码为%s 的服饰出库后，服饰库存为负数，请重新选择出库数量",
                    clothingType.getClothingTypeCodeAndSex(), clothingSize);
        }

        clothingOutbound.setClothingInventoryId(clothing.getClothingInventoryId());
        clothingInventoryService.updateClothingInventory(clothing.getClothingInventoryId(), clothingInventory, clothingOutbound.getUpdateBy());
        clothingOutboundMapper.insertClothingOutbound(clothingOutbound);
        return "SUCCESS";
    }

    /**
     * 修改服饰出库
     *
     * @param clothingOutbound 服饰出库信息
     * @return 结果
     */
    @Override
    @Transactional
    public String updateClothingOutbound(ClothingOutbound clothingOutbound) {
        // 查找要出库的服饰品类
        ClothingInventory currentClothing = clothingInventoryService.selectClothingInventoryByClothingTypeAndSize(
                clothingOutbound.getClothingTypeId(), clothingOutbound.getClothingSize());
        if (null == currentClothing) {
            ClothingType clothingType = clothingTypeService.selectClothingTypeByClothingTypeId(clothingOutbound.getClothingTypeId());
            String clothingSize = ClothingSize.getClothingSizeNameByCode(clothingOutbound.getClothingSize());
            return String.format("当前选择的服饰品类: %s，服饰尺码为： %s 对应的服饰不存在", clothingType.getClothingTypeCodeAndSex(), clothingSize);
        }
        // 上次出库信息
        ClothingOutbound originOutbound = this.selectClothingOutboundByOutboundId(clothingOutbound.getOutboundId());

        // 本次出库服饰类别与之前记录一致
        if (currentClothing.getClothingInventoryId() == originOutbound.getClothingInventoryId()) {
            int clothingInventoryNum = currentClothing.getNowInventory() - clothingOutbound.getOutboundNum() + originOutbound.getOutboundNum();
            if (clothingInventoryNum < 0) {
                String clothingSize = ClothingSize.getClothingSizeNameByCode(clothingOutbound.getClothingSize());
                return String.format("修改服饰品类为%s, 服饰尺码为%s 产品的出库数量后，服饰库存为负数，请重新选择出库数量",
                        currentClothing.getClothingType().getClothingTypeCodeAndSex(), clothingSize);
            }
            clothingInventoryService.updateClothingInventory(currentClothing.getClothingInventoryId(), clothingInventoryNum, clothingOutbound.getUpdateBy());
        } else { // 本次出库服饰类别与之前记录不一致
            ClothingInventory originClothing = clothingInventoryService.selectClothingInventoryByClothingInventoryId(originOutbound.getClothingInventoryId());
            int originInventoryNum = originClothing.getNowInventory() + originOutbound.getOutboundNum();
            int currentInventoryNum = currentClothing.getNowInventory() - clothingOutbound.getOutboundNum();
            if (currentInventoryNum < 0) {
                String clothingSize = ClothingSize.getClothingSizeNameByCode(clothingOutbound.getClothingSize());
                return String.format("修改服饰品类为%s, 服饰尺码为%s 产品的出库数量后，服饰库存为负数，请重新选择出库数量",
                        currentClothing.getClothingType().getClothingTypeCodeAndSex(), clothingSize);
            }
            clothingInventoryService.updateClothingInventory(originClothing.getClothingInventoryId(), originInventoryNum, clothingOutbound.getUpdateBy());
            clothingInventoryService.updateClothingInventory(currentClothing.getClothingInventoryId(), currentInventoryNum, clothingOutbound.getUpdateBy());
        }
        clothingOutbound.setClothingInventoryId(currentClothing.getClothingInventoryId());
        clothingOutboundMapper.updateClothingOutbound(clothingOutbound);
        return "SUCCESS";
    }

    /**
     * 批量删除服饰出库
     *
     * @param outboundIds 需要删除的服饰出库主键
     * @param updateBy   更新者
     * @return 结果
     */
    @Override
    @Transactional
    public String deleteClothingOutboundByOutboundIds(Long[] outboundIds, String updateBy) {
        List<ClothingInventoryVo> clothingInventoryVoList = new ArrayList<>();
        for (Long outboundId : outboundIds) {
            ClothingOutbound clothingoutbound = this.selectClothingOutboundByOutboundId(outboundId);
            ClothingInventory clothing = clothingInventoryService.selectClothingInventoryByClothingInventoryId(clothingoutbound.getClothingInventoryId());
            int clothingInventory = clothing.getNowInventory() + clothingoutbound.getOutboundNum();
            clothingInventoryVoList.add(new ClothingInventoryVo(clothingoutbound.getClothingInventoryId(), clothingInventory, updateBy));
        }

        for (ClothingInventoryVo clothingInventoryVo : clothingInventoryVoList) {
            clothingInventoryService.updateClothingInventory(clothingInventoryVo.getClothingInventoryId(),
                    clothingInventoryVo.getClothingInventory(), clothingInventoryVo.getUpdateBy());
        }
        clothingOutboundMapper.deleteClothingOutboundByOutboundIds(outboundIds);
        return "SUCCESS";
    }
}
